package com.easylinkin.linkappapi.hcmy.controller;

import com.easylinkin.linkappapi.hcmy.entity.AreasourceecologyFunction;
import com.easylinkin.linkappapi.hcmy.service.AreasourceecologyFunctionService;
import com.easylinkin.linkappapi.common.model.RequestModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: linkapp-group-cloud
 * @description: ~
 * @author: chenkaixuan
 * @create: 2021-09-30 10:54
 */
@RestController
@RequestMapping("/areasourceecologyFunction")
public class EcologyFunctionController {
    @Autowired
    AreasourceecologyFunctionService areasourceecologyFunctionService;

    @PostMapping("page")
    public RestMessage page1(@RequestBody RequestModel<AreasourceecologyFunction> requestModel) {
        Assert.notNull(requestModel, "参数不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        return RestBuilders.successBuilder().data(areasourceecologyFunctionService.page(requestModel.getPage(), requestModel.getCustomQueryParams())).build();
    }

    @PostMapping("saveOrUpdate")
    public RestMessage saveOrUpdate(@RequestBody @Valid AreasourceecologyFunction areasourceecologyFunction, BindingResult result) {
        Assert.notNull(areasourceecologyFunction, "参数不能为空");
        if(result.hasErrors() && result.getAllErrors() != null){
            String error = result.getAllErrors().stream().map(m -> m.getDefaultMessage()).collect(Collectors.toList()).toString();
            return RestBuilders.failureBuilder().message(error).build();
        }
        areasourceecologyFunctionService.saveOrUpdateEntity(areasourceecologyFunction);
        return RestBuilders.successBuilder().build();
    }

    @PostMapping("deleteByIds")
    public RestMessage deleteByIds(@RequestBody AreasourceecologyFunction areasourceecologyFunction) {
        Assert.notNull(areasourceecologyFunction, "参数不能为空");
        areasourceecologyFunctionService.deleteByIds(areasourceecologyFunction);
        return RestBuilders.successBuilder().build();
    }

    @PostMapping("findMenuByType")
    public RestMessage findMenuByType(@RequestBody AreasourceecologyFunction areasourceecologyFunction) {
        Assert.notNull(areasourceecologyFunction, "参数不能为空");
        return RestBuilders.successBuilder().data(areasourceecologyFunctionService.findMenuByType(areasourceecologyFunction)).build();
    }
}
